/*
 * Vehicle.cpp
 *
 *  Created on: Aug 26, 2011
 *      Author: erudhes
 */
#include "Vehicle.h"
#include "WxUtils.h"

Vehicle::Vehicle(const wxPoint& position, wxBitmap& bitmap, unsigned int dotSize): mPosition(position), mSpeed(0, 0), mBitmap(bitmap), mDotSize(dotSize)
{
	this->draw();
}

/*virtual*/
Vehicle::~Vehicle()
{
}

std::vector<wxPoint> Vehicle::getValidNextPositions() const
{
	std::vector<wxPoint> result = WxUtils::getPointsAround(mPosition + mSpeed, mDotSize);

	result.push_back(mPosition + mSpeed);

	return result;
}

bool Vehicle::move(const wxPoint& nextPosition)
{
	bool isNextPositionValid = false;
	std::vector<wxPoint> validPositions = this->getValidNextPositions();
	std::vector<wxPoint> points = WxUtils::getPointsAround(mPosition);

	for(unsigned int i = 0; i < validPositions.size(); i++)
	{
		if(nextPosition == validPositions[i])
		{
			isNextPositionValid = true;
			break;
		}
	}
	if(false == isNextPositionValid)
		return false;
	// Erase the vehicle from, the earlier position
	this->draw(true);
	// Assign the new values
	mSpeed = nextPosition - mPosition;
	mPosition = nextPosition;
	this->draw();

	return true;
}

void Vehicle::draw(bool erase)
{
	std::vector<wxPoint> points = WxUtils::getPointsAround(mPosition);
	wxMemoryDC dc(mBitmap);
	wxPen pen;

	if(true == erase)
		pen.SetColour(WxUtils::WHITE.getRed(), WxUtils::WHITE.getGreen(), WxUtils::WHITE.getBlue());
	else
		pen.SetColour(WxUtils::BLACK.getRed(), WxUtils::BLACK.getGreen(), WxUtils::BLACK.getBlue());
	dc.SetPen(pen);
	for(unsigned int i = 0; i < points.size(); i++)
		dc.DrawPoint(points[i]);
}
